// pages/transactionRed/transactionRed.js
const timeFormatting = require('../../utils/timeFormatting.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    receiveMoney:'',
    receiveNum:'',
    sendMoney:'',
    sendNum:'',
    userInfo:'',
    pageSize: 10,
    pageNumber: 1,
    isReturn:false,
    isReturn2:false,
    isOpen:false,
    blessings: '',
    sendUrl: '',
    sendName: '',
    receiveName: '',
    receiveUrl: '',
    redPacket:'',
    sendTime:'',
    isopens:'',
    redid:'',
    birthId:'',
    red:true,
    type:'',
    returninput:'',
  },
  getredList(){
    var that = this
    app.req({
      url: "wechat/birthManagePart/myList",
      method: "post",
      data:{
        pageSize: that.data.pageSize,
        pageNumber: that.data.pageNumber,
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("列表", res)
          var nowList = res.data.myList.rows
          for (var i in nowList) {
            var item = nowList[i];
              if (item.sendTime) {
                var time = new Date((item.sendTime).replace(/-/g, "/")).getTime();
                item.sendTime = timeFormatting.showDate(new Date(time))
              }
          }
          that.setData({
            receiveMoney: res.data.receiveMoney,
            receiveNum: res.data.receiveNum,
            sendMoney: res.data.sendMoney,
            sendNum: res.data.sendNum,
            list: that.data.list.concat(nowList)
          })
          console.log('长度',that.data.list.length)
        }
      }
    })
  },
//我发的红包
  myred(e){
    console.log('点击',e)
    var birthid = e.currentTarget.dataset.birthid
    var id = e.currentTarget.dataset.id
    var isopen = e.currentTarget.dataset.isopen
    var isread = e.currentTarget.dataset.isread
    var type = e.currentTarget.dataset.type 
    var that = this
    that.redxq(id)
    that.setData({
      isReturn:true,
      type:type,
    })
  },
  xClose1(){
    this.setData({
      isReturn:false,
    })
  },
  //收到红包
  getred(e){
    console.log('点击', e)
    var birthid = e.currentTarget.dataset.birthid
    var id = e.currentTarget.dataset.id
    var isopen = e.currentTarget.dataset.isopen
    var isread = e.currentTarget.dataset.isread
    var type = e.currentTarget.dataset.type
    var that = this
    if (isopen == 1 || isopen == 2 || isopen == 4){
      wx.navigateTo({
        url: '/pages/DetailsRedPackets/detailsRedPackets?redId=' + id
      });
      // that.redxq(id)
      // that.setData({
      //   isReturn:true,
      //   type:type,
      // })
    }else{
      that.redxq(id)
      that.setData({
        isOpen: true,
        type:type,
      })
    }
  },
  //红包详情
  redxq(id){
    var that = this
    app.req({
      url: "wechat/birthManagePart/info",
      method: "post",
      data: {
        id: id,
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("红包详情", res)
          that.setData({
            redBlessings: res.data.blessings,
            sendUrl: res.data.sendUrl,
            sendName: res.data.sendName,
            receiveName: res.data.receiveName,
            receiveUrl: res.data.receiveUrl,
            redPacket: res.data.redPacket,
            sendTime: res.data.sendTime.substring(11, 16),
            isopens: res.data.isOpen,
            redid: res.data.id,
            birthId: res.data.birthId,
          })
        }
      }
    })
  },
  //开红包
  openRed() {
    var that = this
    if (that.data.red) {
      that.setData({
        red: false
      })
      app.req({
        url: "wechat/birthManagePart/receiveRedEnvelope",
        method: "post",
        data: {
          id: that.data.redid,
          birthId: that.data.birthId
        },
        success: function (res) {
          if (200 == res.code) {
            that.setData({
              isOpen: false,
              red: true,
              // isReturn:true,
            })
            wx.navigateTo({
              url: '/pages/DetailsRedPackets/detailsRedPackets?redId=' + that.data.redid
            });
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  xClose() {
    this.setData({
      isOpen: false,
    })
  },
  //退回
  returnBtn() {
    var that = this
    that.setData({
      isReturn2: true,
      isOpen: false,
    })
  },
  xClosetwo(){
    this.setData({
      isReturn2: false,
    })
  },
  //感谢
  returninput(e) {
    this.setData({
      returninput: e.detail.value
    })
  },
  //退款
  returnRed() {
    var that = this
    app.req({
      url: "wechat/birthManagePart/retreatRedEnvelope",
      method: "post",
      data: {
        id: that.data.redid,
        birthId: that.data.birthId,
        blessings: that.data.returninput,
      },
      success: function (res) {
        if (200 == res.code) {
          wx.showToast({
            title: "红包已退回",
            duration: 2000
          })
          that.setData({
            isReturn2: false
          })
          that.onShow()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo:userInfo
      })
      console.log('用户',userInfo)
    })
    that.setData({
      pageSize: 10,
      pageNumber: 1,
      list:[],
    })
    that.getredList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      pageSize: that.data.pageSize,
      pageNumber: that.data.pageNumber+1,
    })
    that.getredList()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})